package com.sls.web.manager.action.recharge;

import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.recharge.pojo.StockSupplier;
import com.sls.web.manager.recharge.querybean.StockSupplierQuery;
import com.sls.web.manager.recharge.service.StockSupplierService;

/**
 * @author sxf
 * 
 */
@Results({ @Result(name = "redirect", type = "redirect", location = "/recharge/stock_supplier!list.htm") })
public class StockSupplierAction extends SuperAction {
	private static final long serialVersionUID = 3261699511471941843L;

	@Resource(name = "stockSupplierService")
	private StockSupplierService stockSupplierService;

	private String id;
	private String name;

	// 页面展示
	private Pagination<StockSupplier> page;
	private StockSupplierQuery query;
	private StockSupplier stockSupplier;

	private String message;// ajax 返回数据

	public String list() throws Exception {
		if (query == null) {
			query = new StockSupplierQuery();
			query.setCurrentPage(Constants.INIT_CURRENTPAGE);
			query.setPageSize(Constants.INIT_PAGESIZE);
		}
		page = stockSupplierService.queryPage(query, getCurrentOperator());
		return "list";
	}

	public String doAdd() throws Exception {		
		if (StringUtils.isNotBlank(name)) {
			StockSupplier stockSupplier = new StockSupplier();
			stockSupplier.setId(UUID.randomUUID().toString());
			stockSupplier.setName(name);
			stockSupplier.setCreateOperator(getCurrentOperator().getUserId());
			stockSupplierService.insert(stockSupplier, getCurrentOperator());
		}

		return "redirect";
	}

	public String doEdit() throws Exception {		
		boolean bool = false;
		if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(name)) {
			StockSupplier stockSupplier = new StockSupplier();
			stockSupplier.setId(id);
			stockSupplier.setName(name);
			stockSupplier.setUpdateOperator(getCurrentOperator().getUserId());
			try {
				bool = stockSupplierService.update(stockSupplier,
						getCurrentOperator());
			} catch (Exception e) {

			}
		}

		String result = "";
		if (bool) {
			result = "success";
		} else {
			result = "error";
		}
		message = result;
		return "ajax";
	}

	public String delete() throws Exception {		
		if (StringUtils.isNotBlank(id)) {
			StockSupplier stockSupplier = new StockSupplier();
			stockSupplier.setId(id);
			stockSupplierService.delete(stockSupplier, getCurrentOperator());
		}

		return "redirect";
	}

	public Pagination<StockSupplier> getPage() {
		return page;
	}

	public void setPage(Pagination<StockSupplier> page) {
		this.page = page;
	}

	public StockSupplierQuery getQuery() {
		return query;
	}

	public void setQuery(StockSupplierQuery query) {
		this.query = query;
	}

	public StockSupplier getStockSupplier() {
		return stockSupplier;
	}

	public void setStockSupplier(StockSupplier stockSupplier) {
		this.stockSupplier = stockSupplier;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
